var flag = false;
$(function() {
	$(".login-body").css('height', $(window).height() - 44);
	
	if (/(applewebkitapp)/i.test(navigator.userAgent)) {
		$("#wx_button").show();
		$("#qq_button").show();
	} else if (/(androidapp)/i.test(navigator.userAgent)) {
		$("#wx_button").show();
		$("#qq_button").show();
	} else {
		$("#wx_button").hide();
		$("#qq_button").hide();
	}

	$("#wx_button").click(function(){		
		if (/(applewebkitapp)/i.test(navigator.userAgent)) {
			window.location = 'js-call:weixin_ios_login:';
		} else if (/(androidapp)/i.test(navigator.userAgent)) {
			window.Weixin.login();
		} else {
			
		}
	});
	
	$("#qq_button").click(function(){		
		if (/(applewebkitapp)/i.test(navigator.userAgent)) {
			window.location = 'js-call:weixin_ios_qqlogin:';
		} else if (/(androidapp)/i.test(navigator.userAgent)) {
			window.Weixin.qqlogin();
		} else {
			
		}
	});
	
	if ($.cookie("keeppassword") == "true") {
		$("#keeppassword").attr("checked", true);
		$("#loginname").val($.cookie("loginname"));
		var pwlength = $.cookie("pwlength");
		var pw = '';
		for (var i = 0; i < pwlength; i++) {
			pw += ' ';
		}
		$("#passwd").val(pw);
		flag = true;
	}
	$("#passwd").bind("focus", function(){
		if(flag){
			$(this).val('');
		}
		flag = false;
	});
	
})

function login() {
	var loginnameResult = checkLoginname($("#loginname").val());
	if (!loginnameResult.result) {
		$.alert(loginnameResult.msg);
		return false;
	}
	if(!flag){
		var passwd = $("#passwd").val();
		if (passwd == '') {
			$.alert("密码不能为空");
			return false;
		}
	}
	loginFirst(function(result) {
		if (result.code == '1') {
			$("#key1").val(result.key1);
			loginSubmit(function(result1) {
				if (result1.code == '1') {
					if($("#keeppassword").prop("checked")){
						$.cookie("loginname", $("#loginname").val(), {
							expires : 365 * 2,
							path : '/',
							domain: result1.suffix
						});
						var pw = '';
						if(flag){
							pw = $.cookie('passwd');
						} else {
							pw = $.md5($("#passwd").val() + $("#key1").val());
						}
						$.cookie("passwd", pw, {
							expires : 365 * 2,
							path : '/',
							domain: result1.suffix
						});
						$.cookie("keeppassword", true, {
							expires : 365 * 2,
							path : '/',
							domain: result1.suffix
						});
						$.cookie("pwlength", $("#passwd").val().length, {
							expires : 365 * 2,
							path : '/',
							domain: result1.suffix
						});
					} else {
						$.cookie('loginname', null, {
							path : '/',
							domain : '.xinghaoshangcheng.com',
							expires : -1
						});
						$.cookie('passwd', null, {
							path : '/',
							domain : '.xinghaoshangcheng.com',
							expires : -1
						});
						$.cookie('pwlength', null, {
							path : '/',
							domain : '.xinghaoshangcheng.com',
							expires : -1
						});
						$.cookie('keeppassword', null, {
							path : '/',
							domain : '.xinghaoshangcheng.com',
							expires : -1
						});
					}
					$.cookie("token", result1.token, {
						expires : 365,
						path : '/',
						domain: result1.suffix
					});
					// 本地测试需要加上端口号，另外修改hosts文件例如本机192.168.6.18 abcd123.xinghaoshangcheng.com
					// location.href = result1.domain + ":8080" + appPath + "/center/center.html?token=" + result1.token;
					//location.href = result1.domain + "/center/center.html?token=" + result1.token;
					
					
					
					if (/(applewebkitapp)/i.test(navigator.userAgent)) {
						location.replace("http://www.xinghaoshangcheng.com/center/center.html?token=" + result1.token);
					} else if (/(androidapp)/i.test(navigator.userAgent)) {
						location.replace("http://www.xinghaoshangcheng.com/center/center.html?token=" + result1.token);
					} else {
						location.replace(result1.domain + "/center/center.html?token=" + result1.token);
					}
					
				} else {
					$.alert(result1.msg);
				}
			})
		} else {
			$.alert(result.msg);
		}
	})
}

function loginFirst(callback) {
	$.showLoading();
	$.ajax({
		type : "post",
		url : appPath + "/member/checkLoginname.html",
		data : {
			loginname : $("#loginname").val()
		},
		success : function(result) {
			$.hideLoading();
			callback(result);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.hideLoading();
			$.alert("系统异常，请稍后再试");
		}
	});
}

function loginSubmit(callback) {
	$.showLoading();
	var passwd = '';
	if(flag){
		passwd = $.cookie('passwd');
	} else {
		passwd = $.md5($("#passwd").val() + $("#key1").val());
	}
	$.ajax({
		type : "post",
		url : appPath + "/member/loginsubmit.html",
		data : {
			loginname : $("#loginname").val(),
			passwd : passwd
		},
		success : function(result) {
			$.hideLoading();
			callback(result);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.hideLoading();
			$.alert("系统异常，请稍后再试");
		}
	});
}